class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> st=new Stack<>();
        int n=popped.length;
        int i=0;
        for(int x:pushed){
            st.push(x);
            while(!st.isEmpty()&&popped[i]==st.peek()){
                st.pop();
                i++;
            }
        }
        return i==n;
    }
}